package com.alibaba.aone.casestudy.service.impl;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.aone.casestudy.common.entity.AoneDepart;
import com.alibaba.aone.casestudy.exception.AoneConnectionException;
import com.alibaba.aone.casestudy.service.BaseAoneService;


public class BaseAoneServiceImpl implements BaseAoneService {
    private String     AoneServiceUrl;
    private static Log logger = LogFactory.getLog(BaseAoneServiceImpl.class);

    public void setAoneServiceUrl(String aoneServiceUrl) {
        AoneServiceUrl = aoneServiceUrl;
    }

    public String getAoneServiceUrl() {
        return AoneServiceUrl;
    }

    /**
     * 得到请求的Url
     * 
     * @param request
     * @return
     */
    public String getRequstUrl(String request) {
        StringBuffer sb = new StringBuffer();
        sb.append(AoneServiceUrl);
        sb.append(request);
        return sb.toString();
    }

    /**
     *获取Aone的返回结果
     * 
     * @param request
     * @return
     * @throws AoneConnectionException
     */
    public String getAoneResponse(String request) throws AoneConnectionException {
        HttpClient client = new HttpClient();

        String requestUrl = getRequstUrl(request);
        HttpMethod method = new GetMethod(requestUrl);
        if (logger.isDebugEnabled()) {
            logger.debug("Aone request url is :" + requestUrl);
        }

        try {
            client.executeMethod(method);
            if (HttpURLConnection.HTTP_OK == method.getStatusCode()) {
                String response = method.getResponseBodyAsString();
                return response;
            } else {
                throw new AoneConnectionException("Connection to Aone Service is error,url is :"
                        + requestUrl);
            }
        } catch (HttpException e) {
            throw new AoneConnectionException(e);
        } catch (IOException e) {
            throw new AoneConnectionException(e);
        } finally {
            method.releaseConnection();
            client.getHttpConnectionManager().closeIdleConnections(3000);
        }

    }

    /**
     *获取Aone的返回结果
     * 
     * @param request
     * @return
     * @throws AoneConnectionException
     */
    public String getAoneResponse(String request, String params) throws AoneConnectionException {
        HttpClient client = new HttpClient();

        StringBuffer sb = new StringBuffer();
        sb.append(AoneServiceUrl);
        sb.append(request);
        sb.append(params);
        String requestUrl = sb.toString();
        if (logger.isDebugEnabled())
            logger.debug("Aone request url is :" + requestUrl);
        HttpMethod method = new GetMethod(requestUrl);

        try {
            client.executeMethod(method);
            if (HttpURLConnection.HTTP_OK == method.getStatusCode()) {
                String response = method.getResponseBodyAsString();
                return response;
            } else {
                throw new AoneConnectionException("Connection to Aone Service is error,url is :"
                        + requestUrl);
            }
        } catch (HttpException e) {
            throw new AoneConnectionException(e);
        } catch (IOException e) {
            throw new AoneConnectionException(e);
        } finally {
            method.releaseConnection();
            client.getHttpConnectionManager().closeIdleConnections(3000);
        }

    }

}